/**
 * index.js 主页
 * 
 * @author zhongweizhang@163.com
 * @description 无
 */

function requestPreProcess(conn, response, options) {
	var obj = Ext.decode(response.responseText);
	if (obj.success == false) {
		Ext.MessageBox.alert('错误', obj.errors.msg);
	}
}

Ext.Ajax.on('requestcomplete', requestPreProcess);

Ext.onReady(function() {

			var barpn = new Ext.Panel({
						region : 'north',
						height : 60,
						html : '<center><font size=7 >Sample示例</font></center>'
					});

			var smpleRecord = Ext.data.Record.create([{
						name : 'sampleId',
						mapping : 'sampleId'
					}, {
						name : 'sampleName',
						mapping : 'sampleName'
					}, {
						name : 'sampleDate',
						mapping : 'sampleDate'
					}]);

			var addSample = new Ext.Button({
						text : '添加',
						handler : function() {
							ds.insert(0, new smpleRecord({
												'sampleId' : '',
												'sampleName' : '',
												'sampleDate' : new Date()
											}));

							mainpn.getSelectionModel().selectFirstRow();
						}
					});

			var saveSample = new Ext.Button({
						text : '保存',
						handler : function() {
							rightpn.getForm().submit({
										url : './code/sample!saveSample.action',
										success : function(form, action) {
											ds.load();
										}
									});
						}
					});

			var deleteSample = new Ext.Button({
						text : '删除',
						handler : function() {
							rightpn.getForm().submit({
										url : './code/sample!deleteSample.action',
										success : function(form, action) {
											ds.load();
										}
									});
						}
					});

			var querySample = new Ext.Button({
						text : '查询',
						handler : function() {
							Ext.MessageBox.alert('提示', '这个功能没有写');
						}
					});

			var rightpn = new Ext.form.FormPanel({
						region : 'east',
						width : 400,
						title : 'east',
						items : [new Ext.form.TextField({
											name : 'sampleId',
											emptyText : '请输入sampleId ',
											fieldLabel : '示例ID'
										}), new Ext.form.TextField({
											name : 'sampleName',
											emptyText : '请输入sampleName ',
											fieldLabel : '示例名'
										}), new Ext.form.DateField({
											name : 'sampleDate',
											format : 'Y-m-d',
											emptyText : '请输入sampleDate ',
											fieldLabel : '示例日期'
										})],
						buttons : [addSample, saveSample, deleteSample,
								querySample]
					});

			var cm = new Ext.grid.ColumnModel([{
						header : '示例ID',
						dataIndex : 'sampleId'
					}, {
						header : '示例名',
						dataIndex : 'sampleName'
					}, {
						header : '示例日期',
						dataIndex : 'sampleDate',
						renderer : function(val) {
							if (val)
								return val.format('Y-m-d');
							else
								return '';
						}
					}]);

			var ds = new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
									url : './code/sample!findSample.action'
								}),
						// proxy : new Ext.data.MemoryProxy(data),
						reader : new Ext.data.JsonReader({
									root : 'sampleList'
								}, [{
											name : 'sampleId'
										}, {
											name : 'sampleName'
										}, {
											name : 'sampleDate',
											type : 'date',
											dateFormat : 'Y-m-dTh:i:s'
										}])
					});

			var mainpn = new Ext.grid.GridPanel({
						region : 'center',
						cm : cm,
						ds : ds

					});

			// mainpn.on('cellclick', function(grid, row, col, e) {
			// if (row < 1)
			// return;
			// rightpn.getForm().loadRecord(mainpn.getStore()
			// .getAt(row));
			//
			// });
			ds.on('load', function(store, records) {
						mainpn.getSelectionModel().selectFirstRow();
					});
			mainpn.getSelectionModel().on('rowselect', function(sm, num, rec) {
						rightpn.getForm().loadRecord(rec);
					});

			var vp = new Ext.Viewport({
						layout : 'border',
						items : [barpn, mainpn, rightpn]
					});

			// 载入默认数据
			ds.load();

		});